package org.example.Thread.B_Thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @className: Test
 * @author: 芃
 * @description: 案例1、网图下载
 * @date: 2024/5/13 10:01
 */
class WebDownloader {
    //下载方法
    public void download(String url,String filename) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(filename));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
class DownloadThread extends Thread {
    private String url;
    private String fileName;

    public DownloadThread(String url, String fileName) {
        this.url = url;
        this.fileName = fileName;
    }

    @Override
    public void run() {
        WebDownloader downloader = new WebDownloader();
        downloader.download(url, fileName);
        System.out.println("下载完成" + fileName);
    }
}
public class Test {
    public static void main(String[] args) {
        DownloadThread downloadThread1 = new DownloadThread("https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2905957300.webp","星球崛起4.jpg");
        DownloadThread downloadThread2 = new DownloadThread("https://pics3.baidu.com/feed/b7fd5266d0160924dbe42d537dab3ef7e4cd34ea.jpeg@f_auto?token=ddd95518a2d7f01c1bab545c44745364","复仇者联盟5.jpg");
        DownloadThread downloadThread3 = new DownloadThread("https://pics2.baidu.com/feed/960a304e251f95ca5d2ae4a772155132660952d6.jpeg@f_auto?token=1d63bfa314d680c80e72ac882cde27dd","阿凡达3.jpg");
        downloadThread1.start();
        downloadThread2.start();
        downloadThread3.start();
    }
}
